package com.wsd.smartcarwasher.controller;

import com.wsd.smartcarwasher.constans.Global;
import com.wsd.smartcarwasher.constans.ResultState;
import com.wsd.smartcarwasher.domain.RateInfo;
import com.wsd.smartcarwasher.domain.UserInfo;
import com.wsd.smartcarwasher.service.RateInfoService;
import com.wsd.smartcarwasher.service.WxService;
import com.wsd.smartcarwasher.util.ReturnUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

@Api(tags = "订单评价相关接口")
@Controller
@CrossOrigin
public class RateInfoController {
    @Resource
    private WxService wxService;

    @Resource
    private RateInfoService rateInfoService;

    @ApiOperation(value="添加评论", notes = "订单评价", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "rateInfo", value = "评论信息", required = true, dataType = "RateInfo")
    @RequestMapping(value = "/v1/comment/items", method = RequestMethod.POST, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap addRateInfo(@RequestBody RateInfo rateInfo, HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        rateInfo.setUserId(user.getUserId());
        if (StringUtils.isBlank(rateInfo.getOrderSn())
                || rateInfo.getScore() == 0 || StringUtils.isBlank(rateInfo.getComment())) {
            return ReturnUtil.Error(ResultState.QUERY_PARAM_IS_EMPTY.getMsg());
        }
        int changeRow = rateInfoService.addRateInfo(rateInfo);
        if (changeRow == 0) {
            return ReturnUtil.Error(ResultState.RATE_INFO_ADD_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.RATE_INFO_ADD_SUCCESS.getMsg(), rateInfo);
    }


    @ApiOperation(value="获取用户评论", notes = "订单评价", produces = "application/json", response = ModelMap.class)
    @RequestMapping(value = "/v1/comment/getItems", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getRateInfo(HttpServletRequest request) {
        String sessionId = request.getHeader(Global.AUTHORIZATION);
        UserInfo user = wxService.getUserInfoBySessionId(sessionId);
        if (user == null) {
            return ReturnUtil.Error(ResultState.GET_USER_INFO_FAILED.getMsg());
        }
        return ReturnUtil.Success("获取用户评价", rateInfoService.getRateInfoByUerid(user.getUserId()));
    }


    @ApiOperation(value="删除评论", notes = "订单评价", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "id", value = "评论id", required = true, dataType = "Integer")
    @RequestMapping(value = "/v1/delete_rateInfo", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap addRateInfo(@RequestParam int id) {
        RateInfo rateInfo = rateInfoService.queryRateInfoById(id);
        if (rateInfo == null) {
            return ReturnUtil.Error(ResultState.RATE_INFO_NOT_EXIST.getMsg());
        }
        int changeRow = rateInfoService.deleteRateInfo(id);
        if (changeRow == 0) {
            return ReturnUtil.Error(ResultState.RATE_INFO_DELETE_FAIL.getMsg());
        }
        return ReturnUtil.Success(ResultState.RATE_INFO_DELETE_SUCCESS.getMsg());
    }



    @ApiOperation(value="订单", notes = "订单评价", produces = "application/json", response = ModelMap.class)
    @ApiImplicitParam(name = "id", value = "订单id", required = true, dataType = "String")
    @RequestMapping(value = "/v1/get_rateInfo_by_orderId", method = RequestMethod.GET, produces = "application/json; charset=utf-8")
    @ResponseBody
    public ModelMap getRateInfoByOrderId(@RequestParam String id) {
        RateInfo rateInfo = rateInfoService.queryRateInfoByOrderId(id);
        if (rateInfo == null) {
            return ReturnUtil.Error(ResultState.RATE_INFO_NOT_EXIST.getMsg());
        }
        return ReturnUtil.Success(ResultState.RATE_INFO_DELETE_SUCCESS.getMsg(),rateInfo);
    }

}
